﻿package com.xuanque.mahjongDice.display
{
	import flash.display.*;
	import flash.text.*;
	import flash.events.*;
	
	import com.xuanque.mahjongDice.ScrollBarui.*;
	import utils.transitions.*;
	
	import datas.User;
	
	import com.xuanque.mahjongDice.control.SendPackage;
	import com.xuanque.mahjongDice.ui.QcontextMenu;
	
	/**
	 * ...
	 * @author DefaultUser (Tools -> Custom Arguments...)
	 */
	public class ChatWindow extends Sprite
	{
		private var isClose:Boolean;
		
		public function ChatWindow()
		{
			
			//init();
			
		}
		
		public function init():void {
			
			this.content_txt.text = "";
			//this.content_txt.selectable = false;
			QcontextMenu.getInstance().bulidControl(this.content_txt);
			
			new ScrollTextDealWith(this.content_txt, scrollbarY);
			
			addListener();
			addChildren();
		}
		
		private function addListener():void {
			
			this.chat_mc.send_btn.addEventListener(MouseEvent.CLICK, sendHandler);
			this.chat_mc.send_txt.addEventListener(KeyboardEvent.KEY_DOWN, keySendHandler);
			
		}
		
		
		
		private function addChildren():void {
			this.chat_mc.tip_btn.buttonMode = true;
			this.chat_mc.tip_btn.addEventListener(MouseEvent.CLICK, clickTipHandler);
			
			this.chat_mc.tip_btn.dispatchEvent(new MouseEvent(MouseEvent.CLICK));//默认发布一个事件
			
			textChildren();
			
		}
		
		
		/*
		 * 列表文本控制 
		*/
		private function textChildren():void{
			
			for (var i:int = 1; i <= 7; i++) {
				var list_txt:TextField = this.chat_mc.list_mc["tip" + i + "_txt"];
				
				list_txt.background = false;
				
				list_txt.addEventListener(MouseEvent.ROLL_OVER, textHandler);
				list_txt.addEventListener(MouseEvent.ROLL_OUT, textHandler);
				list_txt.addEventListener(MouseEvent.CLICK, textHandler);
			}
			
		}
		
		
		
		/*
		 * 列表文本 鼠标事件控制 
		*/
		private function textHandler(e:MouseEvent):void{
			var _text:TextField = e.target as TextField;
			var textString:String = _text.text;
			
			switch (e.type) {
				case MouseEvent.ROLL_OVER:
					_text.background = true;
					_text.backgroundColor = 0xCCCCCC;
					break;
				case MouseEvent.ROLL_OUT:
					
					_text.background = false;
					break;
				case MouseEvent.CLICK:
					
					this.chat_mc.tip_btn.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
					
					this.isClose = true;
					
					//this.joinText(textString, User.getInstance().getLoginName()); //直接引用User
					
					_text.background = false;
					this.chat_mc.list_mc.select_mc.y = _text.y;
					
					SendPackage.getInstance().sendWords(textString); // 发 包 (发布的信息)
					
					break;
					
			}
			
		}
		
		
		
		
		/*
		 * 添加文本 content:String , username:String
		 */
		public function joinText(content:String , username:String ,type:String = "chat"):void {
			
			switch (type) {
				
				case "chat" :
					
					//this.content_txt.appendText(username + ": " + content + '\n');
					this.content_txt.htmlText += "<font color='#095A96'>" + username + ": " + content + "\n</font>";
					break;
					
				case "result" :
					
					this.content_txt.htmlText += "<font color='#FF0000'>" + username + ": " + content + "\n</font>";
					break;
					
				case "onlyContent" :
					
					this.content_txt.htmlText += "<font color='#FF0000'>" + content + "\n</font>";
					break;
					
				case "null" :
					
					this.content_txt.appendText('\n'); //换行用
					break;
					
				default:
					
					this.content_txt.appendText('\n'); //换行
					this.content_txt.htmlText += "<font color='#B70000'>" + content + "\n</font>";
				
			}
			content_txt.scrollV = content_txt.maxScrollV;
			
		}
		
		/*
		 * 添加文本 _text:string color:Number
		 */
		public function joinSystemText(content:String , username:String):void {
			
			this.content_txt.htmlText += "<font color='#5B686E'>" + "系统消息: " + username + content + "\n</font>";//统一设置颜色
			
			content_txt.scrollV = content_txt.maxScrollV;
			
		}
		
		
		
		/*
		 * 输入文本的控制 按钮send_btn
		*/
		private function sendHandler(event:MouseEvent = null):void {
			
			if (this.chat_mc.send_txt.text == null || this.chat_mc.send_txt.text == "") {
				
				this.tip_mc.gotoAndPlay(2);
			}
			
			if (this.chat_mc.send_txt.text != null && this.chat_mc.send_txt.text != "") {
				
				if (this.isClose == false) {
					this.chat_mc.tip_btn.dispatchEvent(new MouseEvent(MouseEvent.CLICK));//默认发布一个事件
				}
				
				SendPackage.getInstance().sendWords(this.chat_mc.send_txt.text); // 发 包 (发布的信息)
				
				this.chat_mc.send_txt.text = "";
				
			}
			
			
		}
		
		private function keySendHandler(event:KeyboardEvent):void {
			
			if (event.keyCode == 13) {
				
				sendHandler();
			}
			
		}
		
		
		
		
		/*
		 * tip_btn 被单击处理事件 
		*/
		private function clickTipHandler(e:MouseEvent){
			//var _mc:Movieclip = e.target as Movieclip;
			if(isClose == true){
				this.chat_mc.tip_btn.Icon_mc.gotoAndStop(2);
				Tweener.addTween(this.chat_mc.list_mc, { y:0, time:.5, transition:"easeInOutQuint" } );
				
				this.chat_mc.list_mc.visible = true;
				
			}else{
				this.chat_mc.tip_btn.Icon_mc.gotoAndStop(1);
				Tweener.addTween(this.chat_mc.list_mc, {y: this.chat_mc.list_mc.height + 20, time:.2, transition:"easeInOutQuint",onComplete:this.motionFinish});
				//this.chat_mc.list_mc.visible = false;
			}
			isClose = !isClose;
			
		}
		
		private function motionFinish() {
			
			this.chat_mc.list_mc.visible = false;
		}
		
		
		private function overTextFormat():TextFormat{
			
			var format:TextFormat = new TextFormat();
			format.font = "Verdana";
			format.color = 0xFEFE00;
			format.size = 12;
			return format;
			
		}
		private function defaultTextFormat():TextFormat{
			
			var format:TextFormat = new TextFormat();
			format.font = "Verdana";
			format.color = 0x424332;
			format.size = 12;
			return format;
			
		}
		
		
	}
}